{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "C:\\Users\\zhang C:\\Users\\zhang\\.vntrader\n",
      "C:\\Users\\zhang C:\\Users\\zhang\\.vntrader\n"
     ]
    }
   ],
   "source": [
    "from datetime import datetime\n",
    "import talib\n",
    "import pandas as pd\n",
    "from vnpy.trader.ui import create_qapp, QtCore\n",
    "from vnpy.trader.constant import Exchange, Interval\n",
    "from vnpy.trader.database import database_manager\n",
    "from vnpy.chart import ChartWidget, VolumeItem, CandleItem\n",
    "import os\n",
    "import importlib\n",
    "import traceback\n",
    "from datetime import datetime\n",
    "from threading import Thread\n",
    "from pathlib import Path\n",
    "from inspect import getfile\n",
    "\n",
    "from vnpy.event import Event, EventEngine\n",
    "from vnpy.trader.engine import BaseEngine, MainEngine\n",
    "from vnpy.trader.constant import Interval\n",
    "from vnpy.trader.utility import extract_vt_symbol\n",
    "from vnpy.trader.object import (\n",
    "    HistoryRequest,\n",
    "    LogData\n",
    ")\n",
    "from vnpy.trader.rqdata import rqdata_client\n",
    "from vnpy.trader.database import database_manager\n",
    "from vnpy.app.cta_strategy import CtaTemplate\n",
    "from vnpy.app.cta_strategy.backtesting import (\n",
    "    BacktestingEngine, OptimizationSetting, BacktestingMode\n",
    ")\n",
    "import time"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2021-12-01 00:00:00:2022-03-17 00:00:00\n",
      "query success! 3056\n"
     ]
    }
   ],
   "source": [
    "\n",
    "symbol = \"IF2201\"\n",
    "exchange = Exchange.CFFEX\n",
    "start=datetime(2021, 12, 1)\n",
    "end=datetime(2022, 3, 17)\n",
    "interval=Interval.MINUTE\n",
    "bars = database_manager.load_bar_data(\n",
    "    symbol,\n",
    "    Exchange.CFFEX,\n",
    "    interval=Interval.MINUTE,\n",
    "    start=start,\n",
    "    end=end\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "if not bars or len(bars) == 0:\n",
    "\n",
    "    req = HistoryRequest(\n",
    "        symbol=symbol,\n",
    "        exchange=exchange,\n",
    "        interval=Interval.MINUTE,\n",
    "        start=start,\n",
    "        end=end\n",
    "    )\n",
    "\n",
    "    try:\n",
    "        bars = rqdata_client.query_history(req)\n",
    "\n",
    "        if bars:\n",
    "            database_manager.save_bar_data(bars)\n",
    "            print(f\"{symbol}-{interval}历史数据下载完成\")\n",
    "        else:\n",
    "            print(f\"数据下载失败，无法获取{symbol}的历史数据\")\n",
    "    except Exception:\n",
    "        msg = f\"数据下载失败，触发异常：\\n{traceback.format_exc()}\"\n",
    "        print(msg)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "    import numpy as np\n",
    "    df = pd.DataFrame(bars) \n",
    "    nparray = np.array(bars)\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Model: DbBarData>"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(nparray[0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [],
   "source": [
    "df =pd.DataFrame([s.close_price for s in bars])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [],
   "source": [
    "close_ema = talib.EMA(df[0],20)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0               NaN\n",
       "1               NaN\n",
       "2               NaN\n",
       "3               NaN\n",
       "4               NaN\n",
       "           ...     \n",
       "3051    4775.678356\n",
       "3052    4775.670894\n",
       "3053    4775.664142\n",
       "3054    4775.677081\n",
       "3055    4775.688787\n",
       "Length: 3056, dtype: float64"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\n",
    "close_ema\n",
    "  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "interpreter": {
   "hash": "0d2af4708e901420ca316528daed9d207adf237f59982b81f6b4928a0eab77f9"
  },
  "kernelspec": {
   "display_name": "Python 3.7.1 64-bit (system)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.1"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
